package cn.eastseven.demo.netty.client;

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import org.springframework.stereotype.Component;

import cn.eastseven.demo.netty.common.NettyMessage;

@Sharable
@Component
public class ChatClientHandler extends ChannelHandlerAdapter {

	private ChannelHandlerContext ctx = null;
	
	private String resposeMsg = "";
	
	public ChannelHandlerContext getChannelHandlerContext() {
		return this.ctx;
	}
	
	public String getResposeMsg() {
		return resposeMsg;
	}
	
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		NettyMessage response = (NettyMessage) msg;
		System.out.println("ChatClientHandler.channelRead: "+response);
		Object resp = response.getBody();
		resposeMsg = (String) resp;
		ctx.flush();
	}
	
	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		System.out.println("channelReadComplete: "+resposeMsg);
	}
	
}
